package com.cashmate.struts;

import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.dao.RatesDAO;
import com.cashmate.hibernate.dao.VoucherDAO;
import com.cashmate.hibernate.pojo.Checks;
import com.cashmate.hibernate.pojo.Employees;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;

import com.cashmate.hibernate.pojo.Loans;
import com.cashmate.hibernate.pojo.Rates;
import com.cashmate.hibernate.pojo.Voucher;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


import com.opensymphony.xwork2.ActionSupport;
import java.util.Calendar;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class Release_PrintVoucher extends ActionSupport{
  private LoansDAO loanDAO = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
  private VoucherDAO voucherDAO = (VoucherDAO) ServletActionContext.getServletContext().getAttribute("voucherDAO");
  private RatesDAO rateDAO = (RatesDAO) ServletActionContext.getServletContext().getAttribute("ratesDAO");
  private String selected;
  private String loc;


  @Actions( {
    @Action(value = "/printvouchers", results = {
    })
  })
  public String execute()
  {
      
      Employees empl = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
      
      SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
      SimpleDateFormat format2 = new SimpleDateFormat("MMddyyyy");
      NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.ROOT);
      
      HttpServletResponse response = ServletActionContext.getResponse();
      HttpServletRequest request = ServletActionContext.getRequest();
      
        response.setContentType("application/pdf");
                
        selected = request.getParameter("selected");
        String loans[] = selected.split(",");
        try{
        Rectangle rect = new Rectangle(612, 324);
        Document document = new Document();
        document.setPageSize(rect);
        document.setMargins(36, 36, 10, 10);
        
        PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
        writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);       

        document.open();
        
        StringBuffer javascript = new StringBuffer();
 
        javascript.append("var params = this.getPrintParams();");

        javascript.append("params.interactive =	params.constants.interactionLevel.silent;");
        javascript.append("params.printerName=\"MY_PRINTER_NAME\";");
        javascript.append("params.pageHandling = params.constants.handling.shrink;");

        PdfAction pdfAction= PdfAction.javaScript(javascript.toString(), writer);
        writer.addJavaScript(pdfAction);
        
        Paragraph emp = new Paragraph("");
        
        for(String l : loans){
            Loans loan = new Loans();
            int loanid = 0;
            try{
                loanid = Integer.parseInt(l);
                loan = loanDAO.get(loanid);
            }catch(Exception e){
                
            }
            
            //replaceables
                String branch = loan.getBranches().getBranchname();
                String area = loan.getLoanverification().getAreaname();
                String borrowerId = loan.getBorrowercode();
                String name = loan.getCompletename();
                String addressLine1 = loan.getBorrowers().getStreetaddress() + " " + loan.getBorrowers().getBarangay() +",";
                String addressLine2 = loan.getBorrowers().getCityormunicipality() + ", " + loan.getBorrowers().getProvince() + " " + loan.getBorrowers().getPostalcode();
                String loanAccountNo = loan.getAccount();
                String loanNo = loan.getLoanno() + "";
                
                Voucher voucher = new Voucher();
                for(Voucher v : loan.getVouchers()){
                    voucher = v;
                }
                
                Checks check = new Checks();
                for(Checks c : loan.getCheckses()){
                    check = c;
                }
                
                String voucherNo = voucher.getDocuments().getDocumentno();
                String voucherDate = voucher.getDate();
                String checkNo = check.getCheckno();
                String checkDate = check.getCheckdate();
                String checkAmount = nf.format(check.getCheckamount()).substring(2, nf.format(check.getCheckamount()).length());
                String bankAcctNo = check.getBankaccountno();
                
                double principal;                
                
                principal = loan.getLoanamount();
                DetachedCriteria criteria = criteria = DetachedCriteria.forClass(Rates.class);
                criteria.createCriteria("products", "p").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);          
                criteria.add(Restrictions.eq("p.productid", loan.getLoanverification().getProducts().getProductid()));
                int sizeofrate = rateDAO.countByCriteria(criteria);
                criteria.setProjection(null);
                criteria.setResultTransformer(Criteria.ROOT_ENTITY);
                List<Rates> rates = rateDAO.findByCriteria(criteria, 0, sizeofrate);
                double[] minus = new double[rates.size()];
                String[] minusNames = new String[rates.size()];
                
                double deductions = 0;
                
                for(int o = 0; o < minus.length; o++){
                    
                    if(rates.get(o).getRatename().equalsIgnoreCase("Documentation and Notarial Fee") && rates.get(o).getProducts().getProductname() == 100){
                        minus[o] = (principal * rates.get(o).getRate()) + 10 ;
                    }else{
                        minus[o] = principal * rates.get(o).getRate();
                    }
                    
                    minusNames[o] = rates.get(o).getRatename();                    
                    deductions += minus[o];
                }
                
                double netProceeds = principal - deductions;
                
                String prepare = voucher.getEmployeesByPreparedby().getCompletename();
                String approve = empl.getCompletename();
                String receive = loan.getCompletename();
                String date = format.format(new Date());
            //
                
            PdfPTable voucherTable = new PdfPTable(1);
            voucherTable.setWidthPercentage(100);
            voucherTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            voucherTable.setSpacingBefore(0);
            
            float[] forHead = {8f, 1f, 3f};
            PdfPTable headTable = new PdfPTable(forHead);
            headTable.setWidthPercentage(100);
            headTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            headTable.setSpacingBefore(0);
                        
            PdfPTable head  = new PdfPTable(1);
            head.setWidthPercentage(100);
            head.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            head.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            
            Font fontTitle = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
            Paragraph corpoName = new Paragraph("CASHMATE CREDIT CORPORATION", fontTitle);
            
            Font fontAddress = new Font(FontFamily.HELVETICA, 9, Font.BOLD);
            Paragraph address1 = new Paragraph("Dinato Building, Nancayasan", fontAddress);
            Paragraph address2 = new Paragraph("Urdaneta City 2028", fontAddress);
            
            head.addCell(corpoName);
            head.addCell(address1);
            head.addCell(address2);
                          
            Font fontDoc = new Font(FontFamily.HELVETICA, 10, Font.BOLD);
            PdfPTable doc = new PdfPTable(1);
            doc.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            doc.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            
            PdfPCell type = new PdfPCell(new Paragraph("LOANS DISBURSEMENT VOUCHER", fontDoc));
            type.setBorder(Rectangle.NO_BORDER);
            type.setVerticalAlignment(Element.ALIGN_MIDDLE);
            type.setHorizontalAlignment(Element.ALIGN_CENTER);
            
            PdfPCell fillerTypeBottom = new PdfPCell(emp);
            fillerTypeBottom.setBorder(Rectangle.BOTTOM);
            PdfPCell fillerTypeTop = new PdfPCell(emp);
            fillerTypeTop.setBorder(Rectangle.TOP);
            
            doc.addCell(fillerTypeBottom);
            doc.addCell(type);
            doc.addCell(fillerTypeTop);  
                                    
            PdfPCell docType = new PdfPCell(doc);
            docType.setBorder(Rectangle.NO_BORDER);
            docType.setVerticalAlignment(Element.ALIGN_MIDDLE);
            docType.setHorizontalAlignment(Element.ALIGN_CENTER);
            
            headTable.addCell(head);
            headTable.addCell(new Paragraph(""));
            headTable.addCell(docType);
            
            voucherTable.addCell(headTable);
            
            Font fontInfoHead = new Font(FontFamily.HELVETICA, 8, Font.BOLDITALIC);
            Font fontAddInfo = new Font(FontFamily.HELVETICA, 8, Font.ITALIC);
            Font fontInfo = new Font(FontFamily.HELVETICA, 8);
            
            float[] forBasic = {9f, 1f, 4f};
            PdfPTable basicInfo = new PdfPTable(forBasic);
            basicInfo.getDefaultCell().setBorder(Rectangle.BOTTOM);
            basicInfo.getDefaultCell().setBorderWidthBottom(1);
            basicInfo.setWidthPercentage(100);
            
            float[] forBInfo = {3f, 2f, 3f, 2f, 1f};
            PdfPTable borrowerInfo = new PdfPTable(forBInfo);
            borrowerInfo.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            borrowerInfo.setWidthPercentage(100);
            
            Paragraph headerBranch = new Paragraph("Branch", fontInfoHead);
            Paragraph headerArea = new Paragraph("Area", fontInfoHead);
            Paragraph headerBorrowerID = new Paragraph("Borrower ID", fontInfoHead);
            Paragraph headerBorrowerName = new Paragraph("Name", fontInfoHead);
            Paragraph headerAddress = new Paragraph("Address", fontInfoHead);
            Paragraph headerLoanAcctNo = new Paragraph("Loan Account Number", fontInfoHead);
            Paragraph headerLoanNo = new Paragraph("Loan Number", fontInfoHead);
            
            Paragraph infoBranch = new Paragraph(branch, fontInfo);
            Paragraph infoArea = new Paragraph(area, fontInfo);
            Paragraph infoBorrowerID = new Paragraph(borrowerId, fontInfo);
            Paragraph infoBorrowerName = new Paragraph(name, fontInfo);
            Paragraph infoAddress1 = new Paragraph(addressLine1, fontInfo);
            Paragraph infoAddress2 = new Paragraph(addressLine2, fontInfo);
            Paragraph infoLoanAcctNo = new Paragraph(loanAccountNo, fontInfo);
            Paragraph infoLoanNo = new Paragraph(loanNo, fontInfo);
            
            PdfPCell headerLAN = new PdfPCell(headerLoanAcctNo);
            headerLAN.setBorder(Rectangle.NO_BORDER);
            headerLAN.setColspan(2);
            
            PdfPCell infoNameCell = new PdfPCell(infoBorrowerName);
            infoNameCell.setBorder(Rectangle.NO_BORDER);
            infoNameCell.setColspan(4);
            
            PdfPCell infoAddress1Cell = new PdfPCell(infoAddress1);
            infoAddress1Cell.setBorder(Rectangle.NO_BORDER);
            infoAddress1Cell.setColspan(4);
            
            PdfPCell infoAddress2Cell = new PdfPCell(infoAddress2);
            infoAddress2Cell.setBorder(Rectangle.NO_BORDER);
            infoAddress2Cell.setColspan(4);
            
            borrowerInfo.addCell(headerBranch);
            borrowerInfo.addCell(infoBranch);
            borrowerInfo.addCell(emp);
            borrowerInfo.addCell(headerArea);
            borrowerInfo.addCell(infoArea);
            borrowerInfo.addCell(headerBorrowerID);
            borrowerInfo.addCell(infoBorrowerID);
            borrowerInfo.addCell(emp);
            borrowerInfo.addCell(emp);
            borrowerInfo.addCell(emp);
            borrowerInfo.addCell(headerBorrowerName);
            borrowerInfo.addCell(infoNameCell);
            borrowerInfo.addCell(headerAddress);
            borrowerInfo.addCell(infoAddress1Cell);
            borrowerInfo.addCell(emp);
            borrowerInfo.addCell(infoAddress2Cell);
            borrowerInfo.addCell(headerLAN);
            borrowerInfo.addCell(infoLoanAcctNo);
            borrowerInfo.addCell(headerLoanNo);
            borrowerInfo.addCell(infoLoanNo);
            
            basicInfo.addCell(borrowerInfo);
            basicInfo.addCell(emp);
            
            voucherTable.addCell(basicInfo);
            voucherTable.addCell(emp);
            
            float[] forVInfo = {2f, 1f};
            PdfPTable voucherInfo = new PdfPTable(forVInfo);
            voucherInfo.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            voucherInfo.setWidthPercentage(100);
            voucherInfo.setSpacingAfter(5);
            
            Paragraph headerVoucherNo = new Paragraph("Voucher Number", fontInfoHead);
            Paragraph headerVoucherDate = new Paragraph("Voucher Date", fontInfoHead);
            Paragraph headerCheckNo = new Paragraph("Check Number", fontInfoHead);
            Paragraph headerCheckDate = new Paragraph("Check Date", fontInfoHead);
            Paragraph headerCheckAmount = new Paragraph("Check Amount", fontInfoHead);
            Paragraph headerBankAcctNo = new Paragraph("Bank Account Number", fontInfoHead);
            
            Paragraph infoVoucherNo = new Paragraph(voucherNo, fontInfo);
            Paragraph infoVoucherDate = new Paragraph(voucherDate, fontInfo);
            Paragraph infoCheckNo = new Paragraph(checkNo, fontInfo);
            Paragraph infoCheckDate = new Paragraph(checkDate, fontInfo);
            Paragraph infoCheckAmount = new Paragraph(checkAmount, fontInfo);
            Paragraph infoBankAcctNo = new Paragraph(bankAcctNo, fontInfo);
            
            voucherInfo.addCell(headerVoucherNo);
            voucherInfo.addCell(infoVoucherNo);
            voucherInfo.addCell(headerVoucherDate);
            voucherInfo.addCell(infoVoucherDate);
            voucherInfo.addCell(headerCheckNo);
            voucherInfo.addCell(infoCheckNo);
            voucherInfo.addCell(headerCheckDate);
            voucherInfo.addCell(infoCheckDate);
            voucherInfo.addCell(headerCheckAmount);
            voucherInfo.addCell(infoCheckAmount);
            voucherInfo.addCell(headerBankAcctNo);
            voucherInfo.addCell(infoBankAcctNo);
            
            basicInfo.addCell(voucherInfo);
            
            float[] forAcct = {4f, 2f, 5f};
            PdfPTable accountInfo = new PdfPTable(forAcct);
            accountInfo.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            accountInfo.setWidthPercentage(100);
            
            PdfPTable account = new PdfPTable(3);
            account.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            account.setWidthPercentage(100);
            
            PdfPCell acctBottom = new PdfPCell(emp);
            acctBottom.setBorder(Rectangle.BOTTOM);
            acctBottom.setColspan(3);
            PdfPCell acctNoBorder = new PdfPCell(new Paragraph("   "));
            acctNoBorder.setBorder(Rectangle.BOTTOM);
            acctNoBorder.setBorderWidth(1);
            acctNoBorder.setColspan(3);
            acctNoBorder.setFixedHeight(10);
            
            Paragraph headerAccount = new Paragraph("Account", fontInfoHead);
            Paragraph headerDebit = new Paragraph("Debit", fontInfoHead);
            Paragraph headerCredit = new Paragraph("Credit", fontInfoHead);
            
            PdfPCell headerAccountBottom = new PdfPCell(headerAccount);
            headerAccountBottom.setBorder(Rectangle.BOTTOM);            
            headerAccountBottom.setHorizontalAlignment(Element.ALIGN_CENTER);
            headerAccountBottom.setPaddingBottom(5);
            PdfPCell headerDebitBottom = new PdfPCell(headerDebit);
            headerDebitBottom.setBorder(Rectangle.BOTTOM);      
            headerDebitBottom.setPaddingBottom(5);
            headerDebitBottom.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell headerCreditBottom = new PdfPCell(headerCredit);
            headerCreditBottom.setBorder(Rectangle.BOTTOM);
            headerCreditBottom.setPaddingBottom(5);
            headerCreditBottom.setHorizontalAlignment(Element.ALIGN_CENTER);
            
            Paragraph infoAccount1 = new Paragraph("1003", fontInfo);
            Paragraph infoAccount2 = new Paragraph("4001", fontInfo);
            Paragraph infoAccount3 = new Paragraph("4002", fontInfo);
            Paragraph infoAccount4 = new Paragraph("4003", fontInfo);
            Paragraph infoAccount5 = new Paragraph("4004", fontInfo);
            Paragraph infoAccount6 = new Paragraph("1001", fontInfo);
            
            PdfPCell infoAct1 = new PdfPCell(infoAccount1);
            infoAct1.setBorder(Rectangle.NO_BORDER);            
            infoAct1.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell infoAct2 = new PdfPCell(infoAccount2);
            infoAct2.setBorder(Rectangle.NO_BORDER);            
            infoAct2.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell infoAct3 = new PdfPCell(infoAccount3);
            infoAct3.setBorder(Rectangle.NO_BORDER);            
            infoAct3.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell infoAct4 = new PdfPCell(infoAccount4);
            infoAct4.setBorder(Rectangle.NO_BORDER);            
            infoAct4.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell infoAct5 = new PdfPCell(infoAccount5);
            infoAct5.setBorder(Rectangle.NO_BORDER);            
            infoAct5.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell infoAct6 = new PdfPCell(infoAccount6);
            infoAct6.setBorder(Rectangle.NO_BORDER);            
            infoAct6.setHorizontalAlignment(Element.ALIGN_CENTER);
            
            Paragraph infoDebit1 = new Paragraph(nf.format(principal).substring(2, nf.format(principal).length()), fontInfo);
            Paragraph infoCredit2 = new Paragraph(nf.format(minus[0]).substring(2, nf.format(minus[0]).length()), fontInfo);
            Paragraph infoCredit3 = new Paragraph(nf.format(minus[1]).substring(2, nf.format(minus[1]).length()), fontInfo);
            Paragraph infoCredit4 = new Paragraph(nf.format(minus[2]).substring(2, nf.format(minus[2]).length()), fontInfo);
            Paragraph infoCredit5 = new Paragraph(nf.format(minus[3]).substring(2, nf.format(minus[3]).length()), fontInfo);
            Paragraph infoCredit6 = new Paragraph(nf.format(netProceeds).substring(2, nf.format(netProceeds).length()), fontInfo);
            
            PdfPCell deb1Cell = new PdfPCell(infoDebit1);
            deb1Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            deb1Cell.setBorder(Rectangle.NO_BORDER);
            PdfPCell cred2Cell = new PdfPCell(infoCredit2);
            cred2Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cred2Cell.setBorder(Rectangle.NO_BORDER);
            PdfPCell cred3Cell = new PdfPCell(infoCredit3);
            cred3Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cred3Cell.setBorder(Rectangle.NO_BORDER);
            PdfPCell cred4Cell = new PdfPCell(infoCredit4);
            cred4Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cred4Cell.setBorder(Rectangle.NO_BORDER);
            PdfPCell cred5Cell = new PdfPCell(infoCredit5);
            cred5Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cred5Cell.setBorder(Rectangle.NO_BORDER);
            PdfPCell cred6Cell = new PdfPCell(infoCredit6);
            cred6Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cred6Cell.setBorder(Rectangle.NO_BORDER);
            
            
            account.addCell(acctBottom);
            account.addCell(headerAccountBottom);
            account.addCell(headerDebitBottom);
            account.addCell(headerCreditBottom);
            account.addCell(infoAct1);
            account.addCell(deb1Cell);
            account.addCell(emp);
            account.addCell(infoAct2);
            account.addCell(emp);
            account.addCell(cred2Cell);
            account.addCell(infoAct3);
            account.addCell(emp);
            account.addCell(cred3Cell);
            account.addCell(infoAct4);
            account.addCell(emp);
            account.addCell(cred4Cell);
            account.addCell(infoAct5);
            account.addCell(emp);
            account.addCell(cred5Cell);
            account.addCell(infoAct6);
            account.addCell(emp);
            account.addCell(cred6Cell);
            
            accountInfo.addCell(account);
            accountInfo.addCell(emp);
            
            float[] forLoan = {4f, 1f};
            PdfPTable loanInfo = new PdfPTable(forLoan);
            loanInfo.setWidthPercentage(100);
            loanInfo.getDefaultCell().setBorder(Rectangle.BOTTOM);
            
            Paragraph s = new Paragraph("To payment of loan", fontAddInfo);
            PdfPCell loanCell = new PdfPCell(s);
            loanCell.setBorder(Rectangle.BOTTOM);
            loanCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            loanCell.setVerticalAlignment(Element.ALIGN_TOP);
            loanCell.setColspan(2);
            loanCell.setRowspan(2);
            loanCell.setPaddingBottom(5);
            
            PdfPCell loanBottom = new PdfPCell(emp);
            loanBottom.setBorder(Rectangle.BOTTOM);
            loanBottom.setColspan(2);
            
            Paragraph title1 = new Paragraph("Principal", fontInfo);
            Paragraph title2 = new Paragraph(minusNames[0], fontInfo);
            Paragraph title3 = new Paragraph(minusNames[1], fontInfo);
            Paragraph title4 = new Paragraph(minusNames[2], fontInfo);
            Paragraph title5 = new Paragraph(minusNames[3], fontInfo);
            Paragraph title6 = new Paragraph("Net Proceeds", fontInfo);
            
            Paragraph infoD1 = new Paragraph(nf.format(principal).substring(2, nf.format(principal).length()), fontInfo);
            Paragraph infoC2 = new Paragraph("(" + nf.format(minus[0]).substring(2, nf.format(minus[0]).length()) + ")", fontInfo);
            Paragraph infoC3 = new Paragraph("(" + nf.format(minus[1]).substring(2, nf.format(minus[1]).length()) + ")", fontInfo);
            Paragraph infoC4 = new Paragraph("(" + nf.format(minus[2]).substring(2, nf.format(minus[2]).length()) + ")", fontInfo);
            Paragraph infoC5 = new Paragraph("(" + nf.format(minus[3]).substring(2, nf.format(minus[3]).length()) + ")", fontInfo);
            Paragraph infoC6 = new Paragraph(nf.format(netProceeds).substring(2, nf.format(netProceeds).length()), fontInfo);
            
            PdfPCell titleCell1 = new PdfPCell(title1);
            titleCell1.setBorder(Rectangle.NO_BORDER);
            PdfPCell titleCell2 = new PdfPCell(title2);
            titleCell2.setBorder(Rectangle.NO_BORDER);
            PdfPCell titleCell3 = new PdfPCell(title3);
            titleCell3.setBorder(Rectangle.NO_BORDER);
            PdfPCell titleCell4 = new PdfPCell(title4);
            titleCell4.setBorder(Rectangle.NO_BORDER);  
            PdfPCell lastRow1 = new PdfPCell(title5);
            lastRow1.setBorder(Rectangle.BOTTOM);
            lastRow1.setPaddingBottom(5);
            PdfPCell lastRow2 = new PdfPCell(title6);
            lastRow2.setBorder(Rectangle.BOTTOM);
            lastRow2.setPaddingBottom(5);
            
            PdfPCell d1Cell = new PdfPCell(infoD1);
            d1Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            d1Cell.setBorder(Rectangle.NO_BORDER);
            PdfPCell c2Cell = new PdfPCell(infoC2);
            c2Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            c2Cell.setBorder(Rectangle.NO_BORDER);
            PdfPCell c3Cell = new PdfPCell(infoC3);
            c3Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            c3Cell.setBorder(Rectangle.NO_BORDER);
            PdfPCell c4Cell = new PdfPCell(infoC4);
            c4Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            c4Cell.setBorder(Rectangle.NO_BORDER);
            PdfPCell c5Cell = new PdfPCell(infoC5);
            c5Cell.setPaddingBottom(5);
            c5Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            c5Cell.setBorder(Rectangle.BOTTOM);
            PdfPCell c6Cell = new PdfPCell(infoC6);
            c6Cell.setPaddingBottom(5);
            c6Cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            c6Cell.setBorder(Rectangle.BOTTOM);
            
            loanInfo.addCell(loanBottom);
            loanInfo.addCell(loanCell);
            loanInfo.addCell(titleCell1);
            loanInfo.addCell(d1Cell);
            loanInfo.addCell(titleCell2);
            loanInfo.addCell(c2Cell);
            loanInfo.addCell(titleCell3);
            loanInfo.addCell(c3Cell);
            loanInfo.addCell(titleCell4);
            loanInfo.addCell(c4Cell);
            loanInfo.addCell(title5);
            loanInfo.addCell(c5Cell);
            loanInfo.addCell(title6);
            loanInfo.addCell(c6Cell);
            loanInfo.addCell(emp);
            loanInfo.addCell(emp);
            
            accountInfo.addCell(loanInfo);
            accountInfo.addCell(acctNoBorder);
            
            voucherTable.addCell(accountInfo);
            
            Font fontVerif = new Font(FontFamily.HELVETICA, 8, Font.UNDERLINE);
            
            PdfPTable verifInfo = new PdfPTable(4);
            verifInfo.getDefaultCell().setBorder(Rectangle.TOP);
            verifInfo.getDefaultCell().setBorderWidth(1);
            verifInfo.setWidthPercentage(100);
            verifInfo.setSpacingBefore(10);
            
            Paragraph addPrepare = new Paragraph("Prepared By", fontAddInfo);
            Paragraph addApprove = new Paragraph("Approved By", fontAddInfo);
            Paragraph addReceive = new Paragraph("Received By", fontAddInfo);
            Paragraph addDate = new Paragraph("Date", fontAddInfo);
            
            Paragraph infoPrepare = new Paragraph(prepare, fontVerif);
            Paragraph infoApprove = new Paragraph(approve, fontVerif);
            Paragraph infoReceive = new Paragraph(receive, fontVerif);
            Paragraph infoDate = new Paragraph(date, fontVerif);
            
            PdfPCell addPrepareCell = new PdfPCell(addPrepare);
            addPrepareCell.setBorder(Rectangle.NO_BORDER);
            addPrepareCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell addApproveCell = new PdfPCell(addApprove);
            addApproveCell.setBorder(Rectangle.NO_BORDER);
            addApproveCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell addReceiveCell = new PdfPCell(addReceive);
            addReceiveCell.setBorder(Rectangle.NO_BORDER);
            addReceiveCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell addDateCell = new PdfPCell(addDate);
            addDateCell.setBorder(Rectangle.NO_BORDER);
            addDateCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            
            PdfPCell infoPrepareCell = new PdfPCell(infoPrepare);
            infoPrepareCell.setBorder(Rectangle.NO_BORDER);
            infoPrepareCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell infoApproveCell = new PdfPCell(infoApprove);
            infoApproveCell.setBorder(Rectangle.NO_BORDER);
            infoApproveCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell infoReceiveCell = new PdfPCell(infoReceive);
            infoReceiveCell.setBorder(Rectangle.NO_BORDER);
            infoReceiveCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            PdfPCell infoDateCell = new PdfPCell(infoDate);
            infoDateCell.setBorder(Rectangle.NO_BORDER);
            infoDateCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            
            verifInfo.addCell(addPrepareCell);
            verifInfo.addCell(addApproveCell);            
            verifInfo.addCell(addReceiveCell);
            verifInfo.addCell(addDateCell);
            verifInfo.addCell(infoPrepareCell);
            verifInfo.addCell(infoApproveCell);            
            verifInfo.addCell(infoReceiveCell);
            verifInfo.addCell(infoDateCell);
            
            voucherTable.addCell(verifInfo);
            
            document.add(voucherTable);            
            document.newPage();
            
            voucher.setEmployeesByReviewedby(empl);
            voucher.setEmployeesByCheckedby(empl);
            voucherDAO.update(voucher);
            
            loan.setReleasedate(date);
            
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.DAY_OF_YEAR, 1);
            Date tomorrow = cal.getTime();
            
            loan.setStartofamort(format.format(tomorrow));
            loan.setVoucherprint(true);
            loan.setStatus("Active");
            loanDAO.update(loan);
        }
        
        document.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    
    return NONE;
  }  

    public void setSelected(String selected) {
        this.selected = selected;
    }    

    public String getLoc() {
        return loc;
    }
  

}
